package com.ncmmall.application.business.marketing;

import com.ncmmall.domain.business.marketing.shoppingcard.ShoppingCardRecord;
import com.ncmmall.domain.business.marketing.shoppingcard.ShoppingCardRecordRepository;
import com.ncmmall.domain.security.user.WebUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;

/**
 * 购物卡记录
 */
@Service
@Transactional
public class ShoppingCardRecordApplication {

    @Autowired
    private ShoppingCardRecordRepository shoppingCardRecordRepository;

    /**
     * 赠送购物卡给好友
     *
     * @param record 购物卡
     * @param friend 好友
     */
    public void givenFriend(ShoppingCardRecord record, WebUser friend) {

        ShoppingCardRecord newRecord = new ShoppingCardRecord();
        newRecord.setSource(2);
        newRecord.setUser(friend);
        newRecord.setSender(record.getUser());
        newRecord.setStatus(1);
        newRecord.setBalance(record.getBalance());
        newRecord.setShoppingCard(record.getShoppingCard());
        newRecord.setCreateTime(new Timestamp(System.currentTimeMillis()));
        newRecord.setValidStartTime(record.getValidStartTime());
        newRecord.setValidEndTime(record.getValidEndTime());

        newRecord.setActivateStatus(record.getActivateStatus());
        newRecord.setActivateStart(record.getActivateStart());
        newRecord.setActivateEnd(record.getActivateEnd());

        shoppingCardRecordRepository.save(newRecord);

        record.setStatus(3);
        record.setBalance(0);
        record.setRecipient(friend);
        record.setRecipientTime(newRecord.getCreateTime());
        shoppingCardRecordRepository.save(record);
    }


    /**
     * 购物卡过期激活申请
     *
     * @param record 购物卡
     */
    public void activateApply(ShoppingCardRecord record, String activateApplyNote) {
        record.setActivateApplyNote(activateApplyNote);
        record.setActivateStatus(1);
        record.setActivateApplyTime(new Timestamp(System.currentTimeMillis()));
        shoppingCardRecordRepository.save(record);
    }


    /**
     * 购物卡过期激活审核通过
     *
     * @param record       购物卡
     * @param activateNote 审核备注
     * @param start        激活有效开始时间
     * @param end          激活有效结束时间
     */
    public void activateSuccess(ShoppingCardRecord record, String activateNote, Timestamp start, Timestamp end) {
        record.setActivateNote(activateNote);
        record.setActivateStatus(2);
        record.setValidStartTime(start);
        record.setValidEndTime(end);

        record.setActivateStart(start);
        record.setActivateEnd(end);

        record.setActivateTime(new Timestamp(System.currentTimeMillis()));
        record.setStatus(1);
        shoppingCardRecordRepository.save(record);
    }

    /**
     * 购物卡过期激活审核不通过
     *
     * @param record       购物卡
     * @param activateNote 审核备注
     */
    public void activateFail(ShoppingCardRecord record, String activateNote) {
        record.setActivateNote(activateNote);
        record.setActivateStatus(3);
        record.setActivateTime(new Timestamp(System.currentTimeMillis()));
        shoppingCardRecordRepository.save(record);
    }

}
